package com.grm.sys.service.impl;

import com.grm.common.utils.JwtUtil;
import com.grm.sys.model.SysPerm;
import com.grm.sys.response.HomeResponse;
import com.grm.sys.response.SysPermResponse;
import com.grm.sys.response.SysUserResponse;
import com.grm.sys.service.HomeService;
import com.grm.sys.service.SysPermService;
import com.grm.sys.service.SysUserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysPermService sysPermService;

    @Override
    public HomeResponse getHomeResponse() {
        HomeResponse homeResponse = new HomeResponse();
        Long userId = jwtUtil.getUserId();

        SysUserResponse sysUserResponse = sysUserService.queryById(userId);
        homeResponse.setSysUserResponse(sysUserResponse);

        List<SysPermResponse> sysPermList = queryUserMenuTree(userId);
        homeResponse.setMenuTree(sysPermList);

        List<Map<String, String>> routers = queryUserRouters(userId);
        homeResponse.setRouterList(routers);

        List<String> buttonPermList = queryButtonPermList(userId);
        homeResponse.setButtonPermList(buttonPermList);
        return homeResponse;
    }

    private List<SysPermResponse> queryUserMenuTree(Long userId) {
        return sysPermService.queryUserMenuTree(userId);
    }

    private List<Map<String, String>> queryUserRouters(Long userId) {
        return sysPermService.queryUserRouters(userId);
    }

    private List<String> queryButtonPermList(Long userId) {
        return sysPermService.queryButtonPermList(userId);
    }
}
